#include <iostream>
#include <vector>
#include "SparseTable.h"
#include <fstream>
using namespace std;

void read_file(vector<double>& arr, vector<pair<int, int>>& indexes) {
		ifstream in("in.txt", ifstream::in);
        if (!in.is_open()) 
        { 
			cout<<"File Error"<<endl;
			system("pause");
			exit(1);
        }
        int n;
        in>>n;       
        for ( int i = 0; i < n; ++i ) {
			double number;
			in>>number;
			arr.push_back(number);
        }
		int m;
		in>>m;
		for (int i=0; i<m; i++) {
			int a, b;
			in>>a>>b;
			indexes.push_back(make_pair(a, b));
		}

        in.close();
}

int main() {
	vector<double> arr;
	vector<pair<int, int>> indexes;
	read_file(arr, indexes);

	SparseTable sTable=SparseTable(arr);
	for (int i=0; i<indexes.size(); i++) {
		//cout<<indexes[i].first<<" || "<<indexes[i].second<<endl;
		cout<<sTable.getRMQ(indexes[i].first, indexes[i].second-1)<<endl;
	}
	system("pause");

}